package cn.wjh.business.service.impl;

import cn.wjh.business.domain.Competition;
import cn.wjh.business.domain.CompetitionSign;
import cn.wjh.business.mapper.CompetitionSignMapper;
import cn.wjh.business.service.ICompetitionService;
import cn.wjh.business.service.ICompetitionSignService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 比赛报名Service业务层处理
 *
 * @author jiraWu
 * @date 2023-10-20
 */
@Service
public class CompetitionSignServiceImpl extends ServiceImpl<CompetitionSignMapper, CompetitionSign> implements ICompetitionSignService {
	@Resource
	private CompetitionSignMapper competitionSignMapper;
	@Resource
	private ICompetitionService competitionService;

	/**
	 * 查询比赛报名
	 *
	 * @param signId 比赛报名主键
	 * @return 比赛报名
	 */
	@Override
	public CompetitionSign selectCompetitionSignBySignId(Long signId) {
		return competitionSignMapper.selectCompetitionSignBySignId(signId);
	}

	/**
	 * 查询比赛报名列表
	 *
	 * @param competitionSign 比赛报名
	 * @return 比赛报名
	 */
	@Override
	public List<CompetitionSign> selectCompetitionSignList(CompetitionSign competitionSign) {
		return competitionSignMapper.selectCompetitionSignList(competitionSign);
	}

	/**
	 * 新增比赛报名
	 *
	 * @param competitionSign 比赛报名
	 * @return 结果
	 */
	@Override
	public int insertCompetitionSign(CompetitionSign competitionSign) {
		try {
			/*比赛不存在*/
			Long competitionId = competitionSign.getCompetitionId();//比赛Id
			Competition competition = competitionService.getById(competitionId);//比赛
			if (StringUtils.isNull(competition)) throw new ServiceException("当前比赛不存在,无法报名!");

			Date currentDate = DateUtils.getNowDate();//报名时间
			Date signStartTime = competition.getSignStartTime();
			Date signEndTime = competition.getSignEndTime();
			/*不在报名时间内
			 * currentDate > signStartTime && currentDate < signEndTime */
			boolean flag = currentDate.after(signStartTime) && currentDate.before(signEndTime);
			if (!flag) throw new ServiceException("不在报名时间内,无法报名!", 601);


			Set<String> postsSet = SecurityUtils.getPostsSet();//身份集合
			/*管理员无法参赛*/
			Long userId = SecurityUtils.getUserId();
			if (userId == 1 || postsSet.contains("管理员")) throw new ServiceException("尊敬的管理员,您无法参赛!", 601);

			/*当前赛事评委无法参赛*/
			List<Long> judgeIds = competition.getJudgeIds();
			if (!judgeIds.isEmpty() && judgeIds.contains(userId))
				throw new ServiceException("尊敬的赛事评委,您无法参赛!", 601);

			/*非老师身份无法报名*/
			if (!postsSet.contains("老师")) throw new ServiceException("您非老师身份,无法参赛!", 500);

			/*正常报名*/
			competitionSign.setCreateTime(currentDate);
			competitionSign.setUserId(userId);
			return competitionSignMapper.insertCompetitionSign(competitionSign);
		} catch (DuplicateKeyException duplicateKeyException) {
			throw new ServiceException("您已经报名了哦!", 601);
		}

	}

	/**
	 * 修改比赛报名
	 *
	 * @param competitionSign 比赛报名
	 * @return 结果
	 */
	@Override
	public int updateCompetitionSign(CompetitionSign competitionSign) {
		return competitionSignMapper.updateCompetitionSign(competitionSign);
	}

	/**
	 * 批量删除比赛报名
	 *
	 * @param signIds 需要删除的比赛报名主键
	 * @return 结果
	 */
	@Override
	public int deleteCompetitionSignBySignIds(Long[] signIds) {
		return competitionSignMapper.deleteCompetitionSignBySignIds(signIds);
	}

	/**
	 * 删除比赛报名信息
	 *
	 * @param signId 比赛报名主键
	 * @return 结果
	 */
	@Override
	public int deleteCompetitionSignBySignId(Long signId) {
		return competitionSignMapper.deleteCompetitionSignBySignId(signId);
	}

	/*获取比赛报名状态*/
	@Override
	public boolean isSign(Long competitionId) {
		LambdaQueryWrapper<CompetitionSign> selectWrapper = new QueryWrapper<CompetitionSign>().lambda()
				.eq(CompetitionSign::getCompetitionId, competitionId)
				.eq(CompetitionSign::getUserId, SecurityUtils.getUserId());
		CompetitionSign competitionSign = this.getOne(selectWrapper);
		return StringUtils.isNotNull(competitionSign);
	}
}
